package net.sudot.chess.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.sudot.chess.business.model.Admin;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.service.AdminService;
import net.sudot.chess.business.service.CustomerService;
import net.sudot.commons.json.JsonUtils;
import net.sudot.commons.security.CsrfInterceptor;
import net.sudot.commons.security.JwtLoginUserHandlerInterceptor;
import net.sudot.commons.security.LoginUserHandlerInterceptor;
import net.sudot.commons.security.LoginUserMethodArgumentResolver;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * webmvc 配置
 *
 * @author tangjialin on 2018-01-27.
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Resource
    private CustomerService customerService;
    @Resource
    private AdminService adminService;
    @Resource
    private SystemProperties systemProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截器Interceptor如果要拦截所有请求(如:/**)则可以不用添加
        // 查看org.springframework.web.servlet.handler.MappedInterceptor.matches()可知
        // 若拦截器参数includePatterns为空,则为拦截所有请求
        String[] resourcePathPatterns = new String[]{"/static/**", "/favicon.ico", "/error"};
        registry.addInterceptor(new CsrfInterceptor()).excludePathPatterns(resourcePathPatterns).excludePathPatterns("/wechat/**").excludePathPatterns("/admin/**");
        registry.addInterceptor(new LoginUserHandlerInterceptor(Customer.class, customerService)).excludePathPatterns(resourcePathPatterns).excludePathPatterns("/admin/**");
        registry.addInterceptor(new JwtLoginUserHandlerInterceptor(Admin.class, systemProperties.getJwtSecret(), adminService)).addPathPatterns("/admin/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/", "classpath:/META-INF/resources/");
//        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new LoginUserMethodArgumentResolver());
    }

    @Bean
    public ObjectMapper objectMapper() {
        return JsonUtils.getObjectMapper();
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

}
